package com.gitlab.lucky.eleme.service;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.http.Method;
import com.alibaba.fastjson.JSONObject;
import com.gitlab.lucky.common.redis.RedisService;
import com.gitlab.lucky.common.utils.HttpUtils;
import com.gitlab.lucky.eleme.model.ImageResult;
import com.gitlab.lucky.eleme.model.SendCodeResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.HashMap;

/**
 * @program: lucky
 * @author: Mr.Han
 * @create: 2019-03-13 16:36
 **/
@Service
public class ActivityService {
    public static final Logger LOGGER = LoggerFactory.getLogger(ActivityService.class);
    public static final String SEND_CODE = "https://h5.ele.me/restapi/eus/login/mobile_send_code";
    public static final String CHECK_CODE = "https://h5.ele.me/restapi/eus/v3/captchas";
    public static final String LOGIN = "https://h5.ele.me/restapi/eus/login/login_by_mobile";
    public static final String ACTIVITY = "https://h5.ele.me/restapi/traffic/v2/sudhana/send";
    private static final String SUPER_MEMBER_URL = "https://h5.ele.me/restapi/traffic/v2/sudhana/send";

    /**
     * 发送短信
     *
     * @param phone
     * @return
     * @throws IOException
     */
    public static ImageResult sendMsg(String phone) {
        SendCodeResult sendCodeResult = sendSMS(phone, "", "");
        if (sendCodeResult.getMessage() != null && sendCodeResult.getMessage().equals("账户存在风险,需要图形验证码")) {
            ImageResult imageResult = getImage(phone);
            return imageResult;
        }
        return null;
    }

    public JSONObject lucky(String cookie) {
        String userId = cookie.split(";")[1].replaceAll("USERID=", "");
        HttpRequest body = HttpUtil.createPost(ACTIVITY);
        String result = body
                    .header("x-shard", "loc=117.63825,24.521158")
                    .cookie(cookie)
                    .form("userId", userId)
                    .form("channel", "agent_April_alipay")
                    .form("longitude", "117.63825")
                    .form("latitude", "24.521158")
                    .execute()
                    .body();
        JSONObject jsonObject = JSONObject.parseObject(result);
        return jsonObject;
    }

    /**
     * 领取超级会员
     *
     * @param cookie
     * @return
     */
    public JSONObject superMember(String cookie) {
        String userId = cookie.split(";")[1].replaceAll("USERID=", "");
        String longitude = getTude()[0] + "";
        String latitude = getTude()[1] + "";
        HttpRequest body = HttpUtil.createPost(SUPER_MEMBER_URL);
        String result = body
                    .cookie(cookie)
                    .form("userId", userId)
                    .form("channel", "yizhifu_svip")
                    .form("longitude", longitude)
                    .form("latitude", latitude)
                    .timeout(3000)
                    .execute()
                    .body();
        LOGGER.info("UserId:{},longitude:{},latitude:{},IP:{},Cookie:{}", userId, longitude, latitude, cookie);
        JSONObject jsonObject = JSONObject.parseObject(result);
        return jsonObject;
    }

    /**
     * 随机经纬度
     *
     * @return
     */
    public static BigDecimal[] getTude() {
        //经度
        BigDecimal bigDecimal = RandomUtil.randomBigDecimal(new BigDecimal(116.23), new BigDecimal(118));
        //纬度
        BigDecimal bigDecimal1 = RandomUtil.randomBigDecimal(new BigDecimal(39.54), new BigDecimal(40));
        BigDecimal[] arr = {bigDecimal, bigDecimal1};
        return arr;
    }

    public static String login(String phone, String checkCode, String validateToken) {
        HashMap<String, String> map = new HashMap<>();
        map.put("mobile", phone);
        map.put("validate_code", checkCode);
        map.put("validate_token", validateToken);
        map.put("scf", "ms");
        String body = HttpUtils.sendPost(LOGIN, map);
        return body;
    }

    /**
     * 发送短信
     *
     * @param phone
     * @param captcha_value
     * @param captcha_hash
     * @return
     */
    public static SendCodeResult sendSMS(String phone, String captcha_value, String captcha_hash) {
        String result = HttpUtil.createRequest(Method.POST, SEND_CODE)
                .form("mobile", phone)
                .form("captcha_value", captcha_value)
                .form("captcha_hash", captcha_hash)
                .form("scf", "ms")
                .execute()
                .body();
        SendCodeResult sendCodeResult = JSONObject.parseObject(result, SendCodeResult.class);
        return sendCodeResult;
    }

    /**
     * 从饿了么获取验证码图片链接
     *
     * @param phone
     * @return
     */
    public static ImageResult getImage(String phone) {
        ImageResult imageResult = getImageUrl(phone);
        return imageResult;
    }

    /**
     * 获取图形验证码图片
     *
     * @param phone
     * @return
     */
    public static ImageResult getImageUrl(String phone) {
        String body = HttpUtil.createRequest(Method.POST, CHECK_CODE)
                .form("captcha_str", phone)
                .execute()
                .body();
        ImageResult imageResult = JSONObject.parseObject(body, ImageResult.class);
        return imageResult;
    }
}
